
import Vue from 'vue'
import Vuex from 'vuex'
import VuexPersistence from 'vuex-persist'
 
const vuexLocal = new VuexPersistence({
    storage: window.sessionStorage
})
Vue.use(Vuex)
 
export default new Vuex.Store({
    state : {
        token:"",
        user_info:"",
        freight:{},
        shop_cart:[],
        shop_name:"",
        total:0,
        carriage:0,
        base_url:'http://dxsd.jlxmall.com',
        subscribe:0
    },
    mutations : {
        ADD_USER(state,info){
            console.log(info)
            state.token = info.token
            state.user_info = info.roleinfo
        },
        ADD_SUBSCRIBE(state,subscribe){
            state.subscribe = subscribe;
        },
        LOGIN_OUT(state){
            state.user_info = {}
            state.token = ""
        },
        ADD_ADDRESS(state,data){
            // console.log(state.user_info);
            state.user_info.address = data.address;
            state.user_info.dormitory_name = data.dormitory_name
        },
        ADD_DELIVERY(state,freight){
            // console.log(freight);
            state.freight = freight;
        },
        UPDATE_STATUS(state,data){
            state.user_info.status = data.status;
            state.user_info.remark = data.remark;
            if(data.status == 2){
                state.user_info.type = data.type;
            }
        },
        ADD_CART(state,data){
            state.shop_cart = data;
        },
        DEL_CART(state){
            state.shop_cart = [];
        },
        SHOP_NAME(state,name){
            // console.log(name);
            state.shop_name = name;
        },
        TOTAL(state,total){
            state.total = total;
        },
        CARRIAGE(state,carriage){
            state.carriage = carriage;
        },
        YUE(state,data){
            // console.log(state)
            state.user_info.integral = Math.round((parseFloat(state.user_info.integral) - parseFloat(data))*100)/100;
            
        },
        UPDATE_MONEY(state,data){
            state.user_info.integral = data;
        },
        UPDATE_ADDRESS(state,data){
            state.user_info.address_id = data.id,
            state.user_info.dormitory_name = data.dormitory_name,
            state.user_info.consignee_address = data.address,
            state.user_info.dormitory_id = data.dormitory_id,
            state.user_info.consignee_name = data.name,
            state.user_info.consignee_phone = data.phone
        },
        SET_TYPE(state,data){
            state.user_info.delivery_type = data
        }
    },
    getters:{
        userInfo(state){
            return state.user_info
        },
        token(state){
            return state.token
        },
        freight(state){
            return state.freight
        },
        cart(state){
            return state.shop_cart
        },
        shop_name(state){
            return state.shop_name
        },
        total(state){
            return state.total
        },
        carriage(state){
            return state.carriage
        },
        base_url(state){
            return state.base_url
        },
        subscribe(state){
            return state.subscribe
        }
    },
    plugins: [vuexLocal.plugin]
})
